﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HMS_Hospital_Team2._6.BL;
using HMS_Hospital_Team2._6.DA;
namespace HMS_Hospital_Team2._6.GUI
{
    public partial class UcLoaiPhongBan : UserControl
    {
        public UcLoaiPhongBan()
        {
            InitializeComponent();
        }
        bool checkbutton;
        HMSHospitalEntities context = new HMSHospitalEntities();
        private void btnTaomoi_LoaiPhBan_Click(object sender, EventArgs e)
        {
            grcThongtinloaiPB_LoaiPB.Enabled = true;
            btnSua_LoaiPhBan.Enabled = false;
            txtTenloaiPB_LoaiPhBan.Properties.ReadOnly = false;
            txtMaLoaiPhong_LoaiPB.Properties.ReadOnly = false;
            memoGhiChuLoaiPhBan_LoaiPhBan.Properties.ReadOnly = false;
            chkTrangThai_LoaiPhBan.Properties.ReadOnly = false;
            btnLuu_LoaiPhBan.Enabled = true;
            btnLamMoi_LoaiPhBan.Enabled = true;
            LamMoiLoaiPhongBan();
            checkbutton = true;
        }

        private void UcLoaiPhongBan_Load(object sender, EventArgs e)
        {
            LoaiPhongBanBL loaipban = new LoaiPhongBanBL();
            grcDanhsachLoaiPB_LoaiPB.DataSource = loaipban.LoadDuLieuBL();
        }
        // Hàm xử lý thêm mới loại phòng ban
        void ThemMoiLoaiPhongBan()
        {
            string TenLoaiPhong = txtTenloaiPB_LoaiPhBan.Text.Trim();
            string GhiChu = memoGhiChuLoaiPhBan_LoaiPhBan.Text.Trim();
            string MaLoaiPhong = txtMaLoaiPhong_LoaiPB.Text.Trim();
            bool TrangThai = true;
            if (chkTrangThai_LoaiPhBan.Checked == true)
            {
                TrangThai = true;
            }
            if (chkTrangThai_LoaiPhBan.Checked == false)
            {
                TrangThai = false;
            }
            BL.LoaiPhongBanBL.ThemMoiLoaiPhongBan(MaLoaiPhong,TenLoaiPhong, GhiChu, TrangThai);

        }
        void CapNhatLoaiPhongBan() {
            string MaLoaiPhong = (string)gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0]);
            string TenLoaiPhong = txtTenloaiPB_LoaiPhBan.Text;
            string GhiChu = memoGhiChuLoaiPhBan_LoaiPhBan.Text;
            bool TrangThai = true;
            if (chkTrangThai_LoaiPhBan.Checked == true)
            {
                TrangThai = true;
            }
            if (chkTrangThai_LoaiPhBan.Checked == false)
            {
                TrangThai = false;
            }
            BL.LoaiPhongBanBL.CapNhatLoaiPhongBan(MaLoaiPhong, TenLoaiPhong, GhiChu, TrangThai);
        }

        void LamMoiLoaiPhongBan(){
            txtTenloaiPB_LoaiPhBan.Text = "";
            memoGhiChuLoaiPhBan_LoaiPhBan.Text = "";
            chkTrangThai_LoaiPhBan.Checked = false;

        }
        private void btnLamMoi_LoaiPhBan_Click(object sender, EventArgs e)
        {
            LamMoiLoaiPhongBan();
        }

        private void btnLuu_LoaiPhBan_Click_1(object sender, EventArgs e)
        {
            while (kiemtraloi() == true)
            {
                if (checkbutton == true)
                {
                    ThemMoiLoaiPhongBan();
                    MessageBox.Show("Tạo mới thành công!", "Thông báo!");
                }
                else
                {
                    CapNhatLoaiPhongBan();
                    MessageBox.Show("Sửa thành công!", "Thông báo!");
                }
                //Load lai gridview
                LoaiPhongBanBL loaipban = new LoaiPhongBanBL();
                grcDanhsachLoaiPB_LoaiPB.DataSource = loaipban.LoadDuLieuBL();
                btnTaomoi_LoaiPhBan.Enabled = true;
                LoadThongTinLoaiPB();
                break;
            }
            
           
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            LoadThongTinLoaiPB();
        }

        private void btnSua_LoaiPhBan_Click(object sender, EventArgs e)
        {
            txtTenloaiPB_LoaiPhBan.Properties.ReadOnly = false;
            txtMaLoaiPhong_LoaiPB.Properties.ReadOnly = false;
            memoGhiChuLoaiPhBan_LoaiPhBan.Properties.ReadOnly = false;
            chkTrangThai_LoaiPhBan.Properties.ReadOnly = false;

            btnLuu_LoaiPhBan.Enabled = true; ;
            btnSua_LoaiPhBan.Enabled = false;
            btnLamMoi_LoaiPhBan.Enabled = false;
            btnTaomoi_LoaiPhBan.Enabled = false;
            txtMaLoaiPhong_LoaiPB.Properties.ReadOnly = true;
            //cho biết ng dung đang chỉnh sửa thông tin.
            checkbutton = false;
        }

        public void LoadThongTinLoaiPB()
        {
            //enable datagridview va disable button
            grcDanhsachLoaiPB_LoaiPB.Enabled = true;
            grcThongtinloaiPB_LoaiPB.Enabled = true;
            btnLuu_LoaiPhBan.Enabled = false;
            btnSua_LoaiPhBan.Enabled = true;
            btnLamMoi_LoaiPhBan.Enabled = false;
            //chinh che do Read Only cho textbox, ghi chu va trang thai
            txtTenloaiPB_LoaiPhBan.Properties.ReadOnly = true;
            txtMaLoaiPhong_LoaiPB.Properties.ReadOnly = true;
            memoGhiChuLoaiPhBan_LoaiPhBan.Properties.ReadOnly = true;
            chkTrangThai_LoaiPhBan.Properties.ReadOnly = true;

            using (HMSHospitalEntities show = new HMSHospitalEntities())
            {
                string MaLoaiPhong = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0]).ToString();
                var loaipb = (from pb in show.LoaiPhongBans
                              where pb.MaLoaiPhong == MaLoaiPhong
                              select pb).FirstOrDefault<LoaiPhongBan>();
                txtMaLoaiPhong_LoaiPB.Text = loaipb.MaLoaiPhong;
                txtTenloaiPB_LoaiPhBan.Text = loaipb.TenLoaiPhong;
                memoGhiChuLoaiPhBan_LoaiPhBan.Text = loaipb.GhiChu;
                if (loaipb.TrangThai == true)
                    chkTrangThai_LoaiPhBan.Checked = true;
                else
                    chkTrangThai_LoaiPhBan.Checked = false;
            }
           
        }


        public bool kiemtraloi()
        {
            int count = 0;
            if (txtTenloaiPB_LoaiPhBan.Text.Trim() == "")
            {
                count++;
            }
            if (count == 0)
            {
                return true;
            }
            else
            {
                MessageBox.Show("Vui lòng nhập Tên loại phòng ban!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTenloaiPB_LoaiPhBan.Focus();
                return false;
            }
        }
        private void txtTenloaiPB_LoaiPhBan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar) && !Char.IsLetter(e.KeyChar) && !Char.IsWhiteSpace(e.KeyChar))
                e.Handled = true;
        }
    }
}
